<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%   
String path = request.getContextPath();   
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>新增资源</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<jsp:include page="../inc/bootstrap.jsp"></jsp:include>
	<script type="text/javascript" src="<%=basePath%>static/js/list.js"></script>
	<script type="text/javascript" src="<%=basePath%>static/js/iconPicker.js"></script>
	<script type="text/javascript">
		$(function () {
			$(".icon-picker").iconPicker();
		});
	</script>
</head>
   	<body class="no-skin">
	<!-- 编辑信息结束 -->
	<input type="hidden" id="user_edit" value="0" />
	<form action="<%=basePath %>permission/${urlmsg}.do" name="permissionForm" id="permissionForm" method="post">
	<input type="hidden" id="permission_edit" value="0">
	<%-- <input type="hidden" name="permissionId" id="permissionId" value="${pd.permissionId }"/> --%>
	<input type="hidden" name="permissionType" id="permissionType" value="${pd.permissionType }"/>
	<input type="hidden" name="parentId" id="parentId" value="${null == pd.parentId ? 0:pd.parentId}"/>
	<div class="maxModalIframeCon">
		<table class="table table-striped table-bordered table-condensed">
			<tbody>
				<tr>
					<td class="w120">上级资源</td>
					<td>
						<c:choose>
							<c:when test="${null == pds.permissionName}">
								<input type="text" class="form-control" value="(无) 此项为顶级资源" disabled="disabled"/>
							</c:when>
							<c:otherwise>
								<input type="text" class="form-control" value="${pds.permissionName}[权限标识：${pds.permissionSign }]" disabled="disabled"/>
							</c:otherwise>
						</c:choose>	
					</td>
				</tr>
				
				<tr>
					<td><span class="glyphicon glyphicon-star red"></span>资源名称</td>
					<td>
						<input type="text" class="form-control" name="permissionName" id="permissionName" value="${pd.permissionName}" data-maxlength="24" placeholder="这里输入资源名称" title="名称"/>
					</td>
				</tr>
				
				<tr>
					<td>访问地址</td>
					<td>
						<c:if test="${null != pds.permissionName}">
							<input type="text" name="permissionUrl" id="permissionUrl"
								value="${pd.permissionUrl }" placeholder="资源链接[不填默认#,左侧二级菜单必须输入链接]"
								class="form-control" />
						</c:if>
						<c:if test="${null == pds.permissionName}">
							<input type="text" name="permissionUrl" id="permissionUrl"
								value="" readonly="readonly" placeholder="顶级资源禁止输入"
								class="form-control" />
						</c:if>
					</td>
				</tr>
				
				<tr>
					<td><span class="glyphicon glyphicon-star red"></span>权限标识</td>
					<td>
						<input type="text" name="permissionSign" id="permissionSign"
								value="${pd.permissionSign }" placeholder="权限标识[前缀不能包含上级资源权限标识]"
								class="form-control" data-maxlength="50"/>
					</td>
				</tr>
				
				<tr>
					<td>资源图标</td>
					<td class="position-r">
						<c:if test="${null != pd.permissionIcon}">
							<div style="position: absolute;left: 6px;top: 6px;z-index: 100;width: 26px;height: 26px;background: #efefef;border-right: 1px solid #ddd;">
								<i id="iconShow" class="${pd.permissionIcon}" style="top:6px;left:6px;"></i>
							</div>
						</c:if>
						<input type="text" name="permissionIcon" id="permissionIcon" value="${pd.permissionIcon}"
							 class="icon-picker form-control" style="text-indent:24px;height:28px;" readonly="readonly" placeholder="点击右边选择资源图标"/>
					</td>
				</tr>
				
				<tr>
					<td>排序序号</td>
					<td>
						<input type="number" class="form-control" name="permissionOrder" id="permissionOrder" value="${pd.permissionOrder == null ? 1:pd.permissionOrder}"  placeholder="这里输入排序序号" title="排序""/>
					</td>
				</tr>
				
				<tr>
					<td>资源类型</td>
					<td>
						<label style="float: left;  padding: 0px;padding-left: 8px;">
							<input name="form-field-radio" type="radio"
							id="form-field-radio1"
							<c:if test="${pd.permissionType == '1' || pd.permissionType == null}">checked="checked"</c:if>
							onclick="setType('1','1');" /> <span class="lbl"> 系统菜单</span>
						</label> 
						<label style="float: left; padding: 0px; padding-left: 8px;">
							<input name="form-field-radio" type="radio"
							id="form-field-radio2"
							<c:if test="${pd.permissionType == '2' }">checked="checked"</c:if>
							onclick="setType('1','2');" /> <span class="lbl"> 业务按钮</span>
						</label>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
	<div class="maxModalIframe">
		<button class="btn btn-mini btn-primary" onclick="save('${urlmsg}');" type="button">保存</button>
		<button class="btn btn-mini btn-danger" onclick="top.diag.close();" type="button">取消</button>
	</div>	
	</form>
	<script type="text/javascript">
	//保存
	function save(msg){
		msg = (msg == "edit") ? "资源编辑成功！" : "资源添加成功！";
		if($("#permissionName").val()==""){
			
			$.MaxDialog({
				"title":"标题",
				"type":"tips",
				"content":"请输入资源名称",
				"time":1
			});
			
			$("#permissionName").focus();
			return false;
		}
		
		reg=/^[\u4e00-\u9fa5]*$/;  
		if(!reg.exec($("#permissionName").val())) {   
			$.MaxDialog({
				"title":"通知",
				"type":"tips",
				"content":"资源名称不符合规范，只能输入中文，请重新输入!",
				"time":2
			});
			return false;
		}
		
		if($("#permissionSign").val()==""){
			$.MaxDialog({
				"title":"标题",
				"type":"tips",
				"content":"请输入权限标识",
				"time":1
			});
			$("#permissionSign").focus();
			return false;
		}
		if($("#permissionUrl").val()==""){
			$("#permissionUrl").val('#');
		}
		//类型为空默认为业务类型
		if($("#permissionType").val()==""){
			$("#permissionType").val('1');
		}
		//状态值为空默认为隐藏
		if($("#permission_state").val()==""){
			$("#permission_state").val(1);
		}
		if($("#permissionOrder").val()==""){
			$.MaxDialog({
				"title":"标题",
				"type":"tips",
				"content":"请输入菜单序号",
				"time":1
			});
			$("#permissionOrder").focus();
			return false;
		}
		if(isNaN(Number($("#permissionOrder").val()))){
			$.MaxDialog({
				"title":"标题",
				"type":"tips",
				"content":"请输入菜单序号",
				"time":1
			});
			$("#permissionOrder").focus();
			$("#permissionOrder").val(1);
			return false;
		}
		
		var url = '${urlmsg}';
		$.ajax({
            cache: true,
            type: "POST",
            url:'<%=basePath %>permissionController/'+url+'.do',
            data:$('#permissionForm').serialize(),
            async: false,
            error: function() {
            	showError("服务器异常！");
            },
            success: function(data) {
            	if (data.success) {
					$.MaxDialog({
						"title":"通知",
						"type":"tips",
						"content":"成功！",
						"time":2,
						"hideBefore":function(){
							$("#permission_edit").val("1");
							top.diag.close();
						}
					});
            	} else if (data.msg == 'isRepeat') {
            		$.MaxDialog({
						"title":"通知",
						"type":"tips",
						"content":"权限标识不能重复！",
						"time":2,
						"hideBefore":function(){}
					});
            	}
            }
        });
			
	};
	
	//设置菜单类型or状态
	function setType(type,value){
		if(type == '1'){
			$("#permissionType").val(value);
		}else if(type == '2'){
			$("#permission_state").val(value);
		}else if(type == '3'){
			$("#type").val(value);
		}
	}
	
		//保存
		function save_edit() {
			if ($("#name").val() == "") 
			{
				$.MaxDialog({
					"title":"标题",
					"type":"tips",
					"content":"请输入名称",
					"time":1
				});
				$("#name").focus();
				return false;
			}
			$("#permission_edit").val("1");
			top.diag.close();
			$("#permissionForm").submit();
		};
		
		//判断编码是否存在
		function hasName(parentId){
			var name = $.trim($("#name").val());
			if("" == name)return;
			$.ajax({
				type: "POST",
				url: '<%=basePath%>dictionaries/hasName.do',
		    	data: {name:name,parentId:parentId,tm:new Date().getTime()},
				dataType:'json',
				cache: false,
				success: function(data){
					 if(data.success){
						 $.MaxDialog({
								"title":"标题",
								"type":"tips",
								"content":"名称["+name+"]已存在,重新输入",
								"time":1
							});
						 $('#name').val('');
					 }
				}
			});
		}
	</script>
</body>
</html>